#!/bin/bash
#
# Copyright(c) 2012-2021 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#

# The line below specified that line under it should be used as the test's short description when launching test via run_tests script.
# The text should not be longer than 80 chars - if it is, the script will strip addititonal characters
# DESCRIPTION Write Back - Sequential cache read vs. Sequential cache HDD

# The line below says that this test should be included in BVT - it will be launched, when we use "./run_tests -b"

# USE_IN_NIGHTLY

# Standard beginning for every test - get the main tests directory and
# link the cas_lib file for CAS API, then use "start_test $*" to pass params
# and do other necessary checks and setup.
TESTS_DIR="$(dirname $0)/../"
. $TESTS_DIR/cas_lib
start_test $*

CACHE_DEVICE_SIZE=1G
CORE_DEVICE_SIZE=2G
TEST_DEVICE=${DEVICE_NAME}1-1
TEST_FILE=/tmp/cas.testfile
TEST_BS=32k
TEST_COUNT=16

wb_init() {
    # Use CACHE_DEVICE and CORE_DEVICE provided by configuration file and remove partitions from those devices
    TARGET_DEVICE_OPTION="$CACHE_DEVICE" remove_partitions
    TARGET_DEVICE_OPTION="$CORE_DEVICE" remove_partitions

    # Create 1 primary partitions on CACHE_DEVICE
    TARGET_DEVICE_OPTION="$CACHE_DEVICE" PARTITION_SIZE_OPTION=$CACHE_DEVICE_SIZE PARTITION_IDS_OPTION="1" make_primary_partitions

    # Create 1 primary partitions on CORE_DEVICE
    TARGET_DEVICE_OPTION="$CORE_DEVICE" PARTITION_SIZE_OPTION=$CORE_DEVICE_SIZE PARTITION_IDS_OPTION="1" make_primary_partitions

    # Make ext3 file system
    TARGET_DEVICE_OPTION="${CORE_DEVICE}1" FILESYSTEM_TYPE="ext3" make_filesystem

    # Start cache on CACHE_DEVICE1
    CACHE_MODE_OPTION="wb" CACHE_ID_OPTION="1" CACHE_DEVICE_OPTION="${CACHE_DEVICE}1" start_cache

    # Add a core device using CORE_DEVICE1
    CACHE_ID_OPTION="1" CORE_DEVICE_OPTION="${CORE_DEVICE}1" add_core
}

wb_deinit() {
    # Remove the core device from cache
    CACHE_ID_OPTION="1" CORE_ID_OPTION="1" remove_core

    # Clean up after the test
    CACHE_ID_OPTION="1" stop_cache
}

wb_test() {
    wb_init

    local L_MD5_1
    local L_MD5_2

    test_log_trace "Fill cache, ${TEST_DEVICE}"
    dd if=/dev/urandom of=${TEST_DEVICE} bs=${TEST_BS} count=${TEST_COUNT} oflag=direct

    test_log_trace "Calculate MD5SUM of ${TEST_DEVICE}"
    local T_START=$(date '+%s')
    L_MD5_1=$(md5sum -b ${TEST_DEVICE} | awk '{ print $1 }')
    test_log_trace "MD5 check sum of ${TEST_DEVICE} : $L_MD5_1"
    local T_STOP=$(date '+%s')
    let T_DURATION=${T_STOP}-${T_START}
    test_log_trace "Calculation duration of ${TEST_DEVICE} is $T_DURATION"

    wb_deinit

    # Calculate md5sum of HDD
    test_log_trace "Calculate MD5SUM of ${CORE_DEVICE}1"
    local T_START=$(date '+%s')
    L_MD5_2=$(md5sum -b ${CORE_DEVICE}1 | awk '{ print $1 }')
    test_log_trace "MD5 check sum of ${CORE_DEVICE}1 : $L_MD5_2"
    local T_STOP=$(date '+%s')
    let T_DURATION=${T_STOP}-${T_START}
    test_log_trace "Calculation duration of ${CORE_DEVICE}1 is $T_DURATION"

    if [ "$L_MD5_1" != "$L_MD5_2" ]
    then
        test_log_trace "MD5 sum ERROR"
        test_log_stop
        return 1
    fi

    return 0
}

#
# START TEST
#

test_log_start

run_cmd wb_test

test_log_stop

#
# END TEST
#

# Always return 0 at the end of the test - if at any point something has failed
# in the API functions, test will end and return a proper result.
# If you need to check other things during the test and end the test earlier, you
# should end the test using "end_test $retval" function
end_test 0
